package com.blacktry.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blacktry.entity.easypoidata.DeriveExcel;
import com.blacktry.entity.model.Admin;
import com.blacktry.entity.model.Course;
import com.blacktry.entity.model.CourseDistribution;
import com.blacktry.entity.model.request.CourseRequest;
import com.blacktry.mapper.CourseMapper;
import com.blacktry.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.util.internal.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author blackTry
 * @since 2021-12-05
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Override
    public List<Course> select(Integer id) {
        return null;
    }

    @Override
    public List<Course> selectListByIds(String[] ids) {
        return null;
    }

    @Override
    public List<Course> select() {
        return null;
    }

    @Override
    public Page<Course> selectPage(Course course, Page<Course> coursePage) {
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        // 根据课程名字，老师名字，课程号
        if(course.getCourseId()!=null){
            wrapper.like(Course::getCourseId,course.getCourseId());
        }
        if(course.getCourseName()!=null){
            wrapper.like(Course::getCourseName,course.getCourseName());
        }
        if(course.getCoachTeacher()!=null){
            wrapper.like(Course::getCoachTeacher,course.getCoachTeacher());
        }
        wrapper.orderByAsc(Course::getCourseId);
        return this.baseMapper.selectPage(coursePage,wrapper);
    }

    @Override
    public Integer updateOrInsert(Course course) {
        return null;
    }

    @Override
    public Course selectOne(Integer id) {
        return null;
    }

    @Override
    public Page<Course> dataFlush(Course course, Page<Course> coursePage){
        return this.baseMapper.dataFlush(course, coursePage);
    }

    @Override
    public void clear(){
        this.baseMapper.clear();
    }

    @Transactional
    public void flushClear(){
        Page<Course> page = new Page<>(1,this.baseMapper.pageMax());
        Page<Course> coursePage = this.dataFlush(new Course(),page);
        clear();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        saveBatch(coursePage.getRecords());
    }



    @Override
    public Page<Course> selectCourse(CourseRequest courseRequest, Page<Course> coursePage) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if (!StringUtil.isNullOrEmpty(courseRequest.getCourseId())) {
            wrapper.eq("course_id",courseRequest.getCourseId());
        }
        if (!StringUtil.isNullOrEmpty(courseRequest.getType())) {
            wrapper.eq("type",courseRequest.getType());
        }
        if (!StringUtil.isNullOrEmpty(courseRequest.getCourseName())) {
            wrapper.like("course_name",courseRequest.getCourseName());
        }
        return this.baseMapper.selectPage(coursePage,wrapper);
    }
}
